from django.shortcuts import render
from django.views import generic
from django.contrib.auth.models import User
from django.shortcuts import redirect

from images import models as images_models
from . import models as likes_models


class AddingLike(generic.View):

    def post(self, request):

        user_id = request.POST.get('user_id')
        image_id = request.POST.get('image_id')
        image = images_models.Image.objects.get(id=image_id)
        user = User.objects.get(id=user_id)
        likes_models.Like.objects.create(content_object=image, liked=user)

        return redirect('images:show')


class DeletingLike(generic.View):

    def post(self, request):

        user_id = request.POST.get('user_id')
        image_id = request.POST.get('image_id')
        image = images_models.Image.objects.get(id=image_id)
        user = User.objects.get(id=user_id)
        image.likes.all().get(liked=user).delete()

        return redirect('images:show')
